package com.example.demo.xsgl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/stu")
public class StudentController<T> {
    @PostMapping("/add")
    public Student addStudent(@RequestBody Student student){
        log.info("新增: " + student);
        Student student1 = Student.builder()
                .id(student.id)
                .name(student.name)
                .addminssionDate(student.addminssionDate)
                .major(student.major).build();

        return student1;
    }

    @DeleteMapping("/del/{id}")
    public String deleteStudent(@PathVariable("id")Integer id){
        log.info("Id: " + id + " 的学生信息已删除");
        return "Id: " + id + " 的学生信息已删除";
    }
    @GetMapping("/all")
    public T getStudent(@SuppressWarnings("id") Integer id){
        List<Student> list = new ArrayList<>();

        list.add(Student.builder().id(0L).name("学生一").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());
        list.add(Student.builder().id(1L).name("学生二").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());
        list.add(Student.builder().id(2L).name("学生三").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());
        list.add(Student.builder().id(3L).name("学生四").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());
        list.add(Student.builder().id(4L).name("学生五").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());

        if (id == null){
            return (T) list;
        }else {
            return (T) list.get(id);
        }
    }

    @GetMapping("/all/{id}")
    public Student getStudentXq(@PathVariable("id")Integer id){
        List<Student> list = new ArrayList<>();

        list.add(Student.builder().id(0L).name("学生一").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());
        list.add(Student.builder().id(1L).name("学生二").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());
        list.add(Student.builder().id(2L).name("学生三").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());
        list.add(Student.builder().id(3L).name("学生四").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());
        list.add(Student.builder().id(4L).name("学生五").addminssionDate(LocalDate.of(2021,12,9)).major("移动应用开发").build());

        if(id > list.size() - 1){
            return null;
        }else {
            return list.get(id);
        }
    }

}


